﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HCM_maps_website.Models;

namespace HCM_maps_website.Process.DAL
{
    public class ServiceDAO
    {
        //Datacontext connect database
        MapDataDataContext datacontext = new MapDataDataContext();

        /// <summary>
        /// search service base in keyword
        /// </summary>
        /// <param name="keyword">name or address of service</param>
        /// <returns>list of service</returns>
        public IList<ServiceModel> GetServiceByKeyword(string keyword)
        {
            IList<ServiceModel> services = new List<ServiceModel>();

            var query = datacontext.sp_Place_GetPlaceByKeyword(keyword);

            AssignService(services, query);
             

            return services;
        }

        private static void AssignService(IList<ServiceModel> services, System.Data.Linq.ISingleResult<sp_Place_GetPlaceByIdResult> query)
        {
            foreach (var p in query.ToList())
            {
                ServiceModel temp = new ServiceModel();
                temp.Address = p.Address;
                temp.FullName = p.FullName;
                temp.Info = p.Info;
                temp.Phone = p.Phone;
                temp.Picture = p.Picture;
                temp.Website = p.Website;
                temp.Coordinate = new Coordinate(p.xlocation, p.ylocation);
                temp.Type = ServiceTypeDAO.GetServiceTypeInfo(p.Type);

                services.Add(temp);
            }
        }

        /// <summary>
        /// Find all service in area
        /// </summary>
        /// <param name="start">start point</param>
        /// <param name="end">end point</param>
        /// <returns>list of service</returns>
        public IList<ServiceModel> GetServicesInArea(Coordinate start, Coordinate end)
        {
            IList<ServiceModel> services = new List<ServiceModel>();

            return services;
        }

        /// <summary>
        /// Find services around place or other service
        /// </summary>
        /// <param name="latitude">latitude of point</param>
        /// <param name="longtitude">longtitude of point</param>
        /// <param name="r">Max distance from point to service arounded</param>
        /// <returns>list of service</returns>
        public IList<ServiceModel> GetServiceAround(Coordinate place, float r)
        {
            IList<ServiceModel> services = new List<ServiceModel>();

            var query = datacontext.sp_Place_GetPlaceAround(place.Longitude, place.Latitude, r);

            foreach (var p in query.ToList())
            {
                ServiceModel temp = new ServiceModel();
                temp.Address = p.Address;
                temp.FullName = p.FullName;
                temp.Info = p.Info;
                temp.Phone = p.Phone;
                temp.Picture = p.Picture;
                temp.Website = p.Website;
                temp.Coordinate = new Coordinate(p.xLocation, p.yLocation);
                temp.Type = ServiceTypeDAO.GetServiceTypeInfo(p.Type);

                services.Add(temp);
            }

            return services;
        }

        /// <summary>
        /// Find around filter by type
        /// </summary>
        /// <param name="typeID">id of type sservice wanted to find</param>
        /// <returns>list of service</returns>
        public IList<ServiceModel> GetServiceAroundWithServiceType(int typeID, Coordinate point, float radius)
        {
            IList<ServiceModel> services = new List<ServiceModel>();

            var aroundQuery = datacontext.sp_Place_GetPlaceAroundByPlaceType(typeID, point.Longitude, point.Latitude, radius);

            foreach (var p in aroundQuery.ToList())
            {
                ServiceModel temp = new ServiceModel();
                temp.Address = p.Address;
                temp.FullName = p.FullName;
                temp.Info = p.Info;
                temp.Phone = p.Phone;
                temp.Picture = p.Picture;
                temp.Website = p.Website;
                temp.Coordinate = new Coordinate(p.xLocation, p.yLocation);
                temp.Type = ServiceTypeDAO.GetServiceTypeInfo(p.Type);

                services.Add(temp);
            }

            return services;
        }

        public IList<ServiceModel> GetServiceByPlaceTypeAndDistance(int typeID, Coordinate place, int limit)
        {
            IList<ServiceModel> result = new List<ServiceModel>();
            
            var serviceQuery = datacontext.sp_Place_GetPlaceByPlaceTypeAndDistance(typeID, place.Longitude, place.Longitude, limit);

            AssignService(result, serviceQuery);
            
            return result;
        }

        public List<ServiceModel> GetServiceByTypeAndAddress(int typeID, string address)
        {
            List<ServiceModel> result = new List<ServiceModel>();

            var query = datacontext.sp_Place_GetPlaceByTypeAndAddress(typeID, address);

            AssignService(result, query);

            return result;
        }

        public ServiceType GetPlaceTypeInfo(string description)
        {
            ServiceType type = new ServiceType();

            var query = datacontext.sp_PlaceType_GetPlaceTypeByDescription(description);

            foreach (var t in query)
            {
                type.ID = t.ID;
                type.Description = t.Description;
                type.Picture = t.Picture;
                return type;
            }
            return null;
        }
    }
}